package net.jjjshop.front.controller.lbt;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.LbUserBankEntity;
import net.jjjshop.common.entity.order.Order;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.common.util.BigDecimalUtil;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.front.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;


@Slf4j
@Api(value = "JobController", tags = {"JobController"})
@RestController
@RequestMapping("/front/lbt/job")
public class JobController  {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    // http://127.0.0.1:8890/api/front/lbt/job/getJiesuan?appId=10001
    @ApiOperation(value = "订单结算")
    @GetMapping("/getJiesuan")
    public ApiResult<String> getJiesuan(){
        Order oneOrder = orderService.getOneOrder(1);
        Order editOrder = null;
        User userEntity = null;
        if (oneOrder != null){
            editOrder = new Order();
            editOrder.setOrderId(oneOrder.getOrderId());
            editOrder.setUpdateTime(new Date());
            editOrder.setJs(2);
            orderService.updateById(editOrder);

            Integer uid = oneOrder.getUserId();
            BigDecimal payPrice = oneOrder.getPayPrice();
            //添加自己业绩和订单数量
            User user = userService.getById(uid);
            userEntity = new User();
            userEntity.setAllAv(BigDecimalUtil.addDouble(user.getAllAv(),payPrice));
            userEntity.setOrderNum(user.getOrderNum()+1);
            userEntity.setUserId(user.getUserId());
            userService.updateById(userEntity);

            //计算用户等级  添加上级业绩
            userService.getParentUser(uid,payPrice);

            Order  editOrderJs = new Order();
            editOrderJs.setOrderId(oneOrder.getOrderId());
            editOrderJs.setUpdateTime(new Date());
            editOrderJs.setJs(3);
            orderService.updateById(editOrderJs);

            //计算团队销售奖励
            userService.handleTeamPerformance(uid,payPrice);

        }





        return ApiResult.ok("没有需要结算结算的订单了");
    }

    //计算团队销售奖励
    @GetMapping("/handleTeamPerformance")
    public ApiResult<String> handleTeamPerformance() {


       // userService.handleTeamPerformance(uid,payPrice);
        return ApiResult.ok("没有需要结算结算的订单了");
    }


}
